package cn.com.shopec.erp.product.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.base.service.ISupplierInfoService;
import cn.com.shopec.erp.base.service.impl.SupplierInfoService;
import cn.com.shopec.erp.base.vo.SupplierVo;
import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.NoUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.service.IWhareHouseServer;
import cn.com.shopec.erp.product.entity.DefectReplce;
import cn.com.shopec.erp.product.service.ICommodityService;
import cn.com.shopec.erp.product.service.IDefectReplceService;
import cn.com.shopec.erp.product.service.IDefectiveService;
import cn.com.shopec.erp.product.service.IProductService;
import cn.com.shopec.erp.product.service.IProductStockService;
import cn.com.shopec.erp.product.vo.CommodityVo;
import cn.com.shopec.erp.product.vo.DefectReplceVo;
import cn.com.shopec.erp.product.vo.DefectiveVo;
import cn.com.shopec.erp.product.vo.ProductVo1;
import cn.com.shopec.erp.stock.commo.StockOutUtil;
import cn.com.shopec.erp.stock.vo.StockOutProductVo;
import cn.com.shopec.erp.stock.vo.StockOutVo;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.service.IUserService;
import cn.com.shopec.erp.system.vo.RoleVo;
import net.sf.json.JSONArray;

/**
 * @ClassName: DefectReplceController
 * @Description: 次品退换货管理controller
 * @author: LiangShunWang
 * @date: 2016-11-21 下午16:18:13
 */
@Controller
@RequestMapping("/product/defectiveReplacement")
public class DefectReplceController {

	private static Log log = LogFactory.getLog(CommodityCatController.class);

	@Resource
	private IDefectReplceService defectReplceService;

	@Resource
	private IDefectiveService defectiveService;

	@Resource
	private IUserService userService;

	@Resource
	private ICommodityService commodityService;

	@Resource
	private IWhareHouseServer iWhareHouseServer;

	@Resource
	private ISupplierInfoService supService;

	@Resource
	private IProductService productService;

	@Resource
	private IProductStockService productStockService;

	@Resource
	private SupplierInfoService supplierInfoService;

	/**
	 * 
	 * @Title: toListDefective
	 * @Description: 跳转到次品退换货列表页面
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws Exception
	 * @throws @author:LiangShunWang
	 * @date: 2016-11-21下午16:31:46
	 */
	@RequestMapping("/to_list_replacement")
	public String toListDefective(DefectReplceVo defectReplceVo, ModelMap map) {
		map.put("defectReplceVo", defectReplceVo);
		return "/product/defectReplce_list";
	}

	/**
	 * 
	 * @Title: listBrand
	 * @Description: 分页查询次品退换货信息
	 * @param brandVo
	 * @param pageQuery
	 * @param map
	 * @return
	 * @throws @author: LiangShunWang
	 * @date: 2013-5-27下午01:33:30
	 */
	@RequestMapping("/list_replacement")
	@ResponseBody
	public String listDefective(DefectReplceVo defectReplceVo, PageQuery pageQuery, ModelMap map) {
		try {
			PageFinder<DefectReplceVo> pagedBrandList = this.defectReplceService.getPagedBrandList(defectReplceVo,
					pageQuery);
			map.put("Rows", pagedBrandList.getData());
			map.put("Total", pagedBrandList.getRowCount());
		} catch (Exception e) {
			log.error("listBrand:" + e.getMessage(), e);
		}
		return JsonHelper2.toJson(map);

	}

	/**
	 * 
	 * @Title: toAddBrand
	 * @Description: 跳转到添加次品页面
	 * @return
	 * @throws @author: LiangShunWang
	 * @date: 2016-11-21下午19:21:57
	 */
	@RequestMapping("/to_add_defectReplce")
	public String toAddDefective(PageQuery pageQuery, ModelMap map) {
		map.put("whareHouse", iWhareHouseServer.getPagedCollectSetList(pageQuery).getData());
		map.put("supplier", supService.getPagedCollectSetList(pageQuery).getData());
		return "/product/defectReplce_add";
	}

	/**
	 * 
	 * @Title: addBrand
	 * @Description: 添加次品信息
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws Exception
	 * @throws @author:LiangShunWang
	 * @date: 2016-11-21下午19:16:58
	 */
	@RequestMapping("/add_defectReplce")
	@ResponseBody
	public String addDefective(DefectReplceVo defectReplceVo, String productlist, ModelMap map) {
		try {
			JSONArray jsonArray = JSONArray.fromObject(productlist);
			@SuppressWarnings("unchecked")
			List<DefectReplce> stockInProducts = (List<DefectReplce>) JSONArray.toList(jsonArray, DefectReplce.class);
			this.defectReplceService.addDefectReplce(stockInProducts, defectReplceVo);

		} catch (Exception e) {
			log.error("addBrand:" + e.getMessage(), e);
			return SZSCMConstant.FAIL;
		}
		return SZSCMConstant.SUCCESS;
	}

	/**
	 * 
	 * @Title: deleteDefective
	 * @Description: 删除次品退换货信息
	 * @param brandVo
	 * @return
	 * @throws @author: LiangShunWang
	 * @date: 2013-6-6下午20:48:51
	 */
	@RequestMapping("deleteDefective")
	@ResponseBody
	public String deleteBrand(String defectiveReplceNo) {
		try {
//			this.defectiveService.delDefective(DefectiveVo.getId());
			if(this.defectReplceService.delDefectiveReplce(defectiveReplceNo)){
				return SZSCMConstant.SUCCESS;
			}
		} catch (Exception e) {
			log.error("deleteBrand:" + e.getMessage(), e);
			return SZSCMConstant.FAIL;
		}
		return SZSCMConstant.FAIL;
	}

	/**
	 * 跳至商品列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("toProductList")
	public String toPageCommodityList(CommodityVo commodityVo, PageQuery pageQuery, ModelMap map) throws Exception {
		List<RoleVo> roleVos = this.userService.getOwnRoleVos(UserUtils.getUserVo());
		map.put("roleVos", roleVos);
		map.put("commodity", commodityVo);
		return "/product/spec/commodity_list";
	}

	/**
	 * 分页查询商品列表
	 * 
	 * @param commodity1
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping({ "aJaxPageCommodityList" })
	@ResponseBody
	public String aJaxPageCommodityList(CommodityVo commodityVo, PageQuery pageQuery, ModelMap map) {
		JSONObject jsonObj = new JSONObject();
		try {
			PageFinder<CommodityVo> pageFinder = this.commodityService.pageFinderCommodity(commodityVo, pageQuery);
			jsonObj.put("Rows", pageFinder.getData());
			jsonObj.put("Total", pageFinder.getRowCount());
		} catch (Exception e) {
			log.error("aJaxPageCommodityList:" + e);
		}
		return jsonObj.toJSONString();
	}

	/**
	 * @methodName：getCommodityList
	 * @Description: 进入选择商品页面
	 * @param ProductVo
	 *            pageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("/getCommodityList")
	public ModelAndView getCommodityList(DefectiveVo defectiveVo, PageQuery pageQuery, ModelMap model)
			throws Exception {
		model.put("defectiveVo", defectiveVo);
		return new ModelAndView("product/defectReplce_elect", model);
	}

	/**
	 * @methodName：getAjaxCommodityList
	 * @Description: 获取商品
	 * @param ProductVo
	 *            pageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("/getAjaxCommodityList")
	@ResponseBody
	public String productStockPageList(DefectiveVo defectiveVo, PageQuery pageQuery) throws Exception {
		defectiveVo.setStorageState(ProductSCMConstant.StorageState_DEFECT);// 设置状态为审核状态
		PageFinder<DefectiveVo> page = this.defectiveService.getPagedBrandList(defectiveVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}

	/**
	 * @Title: showPoDetail
	 * @Description: 展现次品退貨单详情
	 * @param poNo
	 * @return
	 * @throws Exception
	 * @throws @author:liang shun wang
	 * @date: 2013-5-27下午2:33:39
	 */
	@RequestMapping("showPoDetail")
	public ModelAndView showPoDetail(String DefectReplceId, String num, String showList, ModelMap modelMap)
			throws Exception {

		DefectReplceVo po = this.defectReplceService.getPurchaseOrder(DefectReplceId);
		modelMap.put("po", po);
		modelMap.put("num", num);
		modelMap.put("showList", StringUtil.isBlank(showList) ? "prods" : showList);
		return new ModelAndView("product/defectRe_detail");
	}

	/**
	 * @Title: ajaxToPoProductList
	 * @Description: 根据采购单号，取得采购单的货品信息，以json方式返回
	 * @param poNo
	 * @param pageQuery
	 * @param modelMap
	 * @return
	 * @throws Exception
	 * @throws @author: liang shun wang
	 * @date: 2013-5-27下午3:14:28
	 */
	@RequestMapping("ajaxToPoProductList")
	@ResponseBody
	public String ajaxToPoProductList(String DefectReplceId, PageQuery pageQuery, ModelMap modelMap) throws Exception {
		JSONObject jsonObj = new JSONObject();
		DefectReplceVo defectReplceVo = new DefectReplceVo();
		defectReplceVo.setDefectReplceId(DefectReplceId);
		PageFinder<DefectReplceVo> pageFinder = this.defectReplceService.getPagedBrandList(defectReplceVo, pageQuery);
		List<StockOutProductVo> stockOutProductVo = StockOutUtil
				.parseStockOutProductVoForDefectReplce(pageFinder.getData());
		jsonObj.put("Rows", stockOutProductVo);
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}

	/**
	 * toAddStockInForPurchase
	 * 
	 * @Description:跳转出库
	 * @param poProd
	 * @return
	 * @throws Exception
	 * @throws @author:liang shun wang
	 * @date: 2016-12-1下午7:15:16
	 */
	@RequestMapping("/AddStockOutForPurchase")
	public String toAddStockInForPurchase(String tabId, String defectReplceId, PageQuery pageQuery, ModelMap map)
			throws Exception {
		// 根据次品退货编号查询该笔退货单
		PageFinder<DefectReplceVo> purchase = defectReplceService.getDefectReplceById(defectReplceId, pageQuery);
		// 根据供应商编号查询供应商信息
		SupplierInfo sup = supplierInfoService.getSupplierInfoById(purchase.getData().get(0).getSupCode());
		purchase.getData().get(0).setAddress(sup.getAddress());// 地址
		purchase.getData().get(0).setLinkman(sup.getLinkman());// 收货人
		purchase.getData().get(0).setReceiverTel(sup.getTelephoneNum());// 电话号码
		DefectReplceVo purchaseReturnOrderVo = (DefectReplceVo) purchase.getData().get(0);
		StockOutVo stockout = StockOutUtil.purchaseReturnParseStockOutVo(purchaseReturnOrderVo);
		stockout.setStockOutNo(NoUtil.getNo(NoUtil.STOCKOUT_NO));
		stockout.setStockOutType(SZSCMConstant.STOCKOUT_TYPE_3);
		// map.put("poNo", purchaseReturnOrderVo.getDefectReplceId());
		map.put("tabId", tabId);
		map.put("stockOut", stockout);
		map.put("stockOutViewType", SZSCMConstant.STOCK_DEFECT_OUT_VIEW);
		return "/stock/stockOut_detail";
	}
	
	/**
	 * @methodName：getCommodityList
	 * @Description: 进入选择供应商页面
	 * @param ProductVo pageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("getSupplierInfoList")
	public ModelAndView getSupplierInfoList(ProductVo1 productVo, PageQuery pageQuery, ModelMap model)
			throws Exception {
		model.put("productVo", productVo);
		return new ModelAndView("/product/defectReplce_supplierInfo_elect", model);
	}

	/**
	 * @methodName：getAjaxCommodityList
	 * @Description: 获取供应商
	 * @param ProductVo pageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("getAjaxSupplierInfoList")
	@ResponseBody
	public String getAjaxSupplierInfoList(SupplierVo productVo, PageQuery pageQuery) throws Exception {
		// PageFinder<ProductVo1> productVoList =
		// this.productService.getPagedProductListByProduct(productVo,
		// pageQuery);
		PageFinder<SupplierInfo> productVoList = this.supplierInfoService.querySupplier(productVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", productVoList.getData());
		map.put("Total", productVoList.getRowCount());
		return JsonHelper2.toJson(map);
	}
}